package com.servertest1;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

public class DatabaseHelper {
	private static final String DATABASE_NAME = "MadMines.db";
	private static final int DATABASE_VERSION = 3;
	private static final String TABLE_NAME = "Account";
	private static final String BOARD_TABLE_NAME = "gamesInPlay";
	private Context context;
	private SQLiteDatabase db;
	private SQLiteStatement insertStmt;
	private SQLiteStatement insertBoardStmt;
	private static final String INSERT = "insert into " + TABLE_NAME + "(name) values (?)" ;
	private static final String INSERT_BOARD = "insert into " + BOARD_TABLE_NAME + 
			" (board_id, board, board_cover, time_remaining, score) values (?, ?, ?, ?, ?)";
	private MadMinesOpenHelper openHelper;
	

	public DatabaseHelper(Context context) {
		this.context = context;
		openHelper = new MadMinesOpenHelper(this.context);
		this.db = openHelper.getWritableDatabase();
		this.insertStmt = this.db.compileStatement(INSERT);
		this.insertBoardStmt = this.db.compileStatement(INSERT_BOARD);
	}

	public long insert(String name) {
		this.insertStmt.bindString(1, name);
		return this.insertStmt.executeInsert();	
	}
	
	public long insertBoard(int board_id, String board, String board_cover, int time_remaining, int score) {
		Cursor cursor = this.db.query(BOARD_TABLE_NAME, new String[]{"board_id"},
				"board_id=?", new String[]{Integer.toString(board_id)},
				null, null, null);
		
		if(cursor.getCount() != 0) {
			ContentValues cv = new ContentValues();
			cv.put("board", board);
			cv.put("board_cover", board_cover);
			cv.put("time_remaining", time_remaining);
			cv.put("score", score);
			
			return this.db.update(BOARD_TABLE_NAME, cv, "board_id=?", new String[]{Integer.toString(board_id)});
			
		}
		
		this.insertBoardStmt.bindLong(1, board_id);
		this.insertBoardStmt.bindString(2, board);
		this.insertBoardStmt.bindString(3, board_cover);
		this.insertBoardStmt.bindLong(4, time_remaining);
		this.insertBoardStmt.bindLong(5, score);
		
		return this.insertBoardStmt.executeInsert();
	}
	
	public boolean deleteBoardData(int game_id) {
		System.out.println("TRING TO DELETE GAME ID: " + game_id);
		Cursor cursor = this.db.query(BOARD_TABLE_NAME, new String[]{"board_id"},
				"board_id=?", new String[]{Integer.toString(game_id)},
				null, null, null);
		
		System.out.println("CURSOR COUNT: " + cursor.getCount());
		return this.db.delete(BOARD_TABLE_NAME, "board_id=" + game_id, null) > 0;
	}
	
	public void closeDB()
	{
		insertStmt.close();
		db.close();
	}
	
	public void deleteAll() {
		this.db.delete(TABLE_NAME, null, null);
	}
	
	public boolean isEmpty(){
		Cursor cursor = this.db.query(TABLE_NAME, new String[]{"name"}, null, null, null, null, null);
		
		if(cursor.getCount() == 0)
		{	
			cursor.close();
			return true;
		}
		cursor.close();
		return false;
		
	}
	

	
	public String getUser(){
		Cursor cursor = this.db.query(TABLE_NAME, new String[]{"name"}, null, null, null, null, null);
		cursor.moveToFirst();
		String name = cursor.getString(0);
		cursor.close();
		return name;
	}
	
	public String[] getBoardData(int game_id){
		Cursor cursor = this.db.query(BOARD_TABLE_NAME, new String[]{"board", "board_cover", "time_remaining, score"},
						"board_id=?", new String[]{Integer.toString(game_id)},
						null, null, null);
		if(cursor.getCount() == 0) {
			String[] toReturn = new String[1];
			toReturn[0] = "no_result";
			return toReturn;
		}
		
		cursor.moveToFirst();
		String[] boardData = new String[4];
		
		boardData[0] = cursor.getString(0);
		boardData[1] = cursor.getString(1);
		boardData[2] = Integer.toString(cursor.getInt(2));
		boardData[3] = Integer.toString(cursor.getInt(3));
		
		cursor.close();
		return boardData;
	}

	public void close() {
	    // NOTE: openHelper must now be a member of CallDataHelper;
	    // you currently have it as a local in your constructor
	    if (openHelper != null) {
	        openHelper.close();
	    }
	}

	
	private static class MadMinesOpenHelper extends SQLiteOpenHelper {
		MadMinesOpenHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, name TEXT)");
			System.out.println("CREATING TABLE");
			db.execSQL("CREATE TABLE " + BOARD_TABLE_NAME + "(id INTEGER PRIMARY KEY, board_id INTEGER, " +
					   "board TEXT, board_cover TEXT, time_remaining INTEGER, score INTEGER)");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

			Log.w("Example", "Upgrading database; this will drop and recreate the tables.");
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
			db.execSQL("DROP TABLE IF EXISTS " + BOARD_TABLE_NAME);
			onCreate(db);
		}
	}
}